/*******************************************************************************
 * Copyright 2011, 2012 Chris Banes.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package uk.co.senab.photoview;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

import java.util.ArrayList;
import java.util.List;

public class PhotoViewPagerActivity extends Activity implements View.OnClickListener {
	public static final String EXTRA_IMAGE_ARRAY = "ImageArray";
	public static final String EXTRA_CURRENT_INDEX = "CurrentIndex";
	private ViewPager mViewPager;
	
    @Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.vpv_photo_view_pager);
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
		ArrayList<String> urls = getIntent().getStringArrayListExtra(EXTRA_IMAGE_ARRAY);
		int index = getIntent().getIntExtra(EXTRA_CURRENT_INDEX,0);
		if (urls==null){
			urls=new ArrayList<>();
		}
		mViewPager.setAdapter(new SamplePagerAdapter(this,urls));
		mViewPager.setCurrentItem(index);
	}

	@Override
	public void onClick(View view) {
		if (view.getId()==R.id.view_pager){
			finish();
		}
	}

	static class SamplePagerAdapter extends PagerAdapter {

		private List<String> imageUrls;
		private Activity activity;


		public SamplePagerAdapter( Activity activity,List<String> imageUrls) {
			this.imageUrls = imageUrls;
			this.activity = activity;
		}

		@Override
		public int getCount() {
			return imageUrls.size();
		}

		@Override
		public View instantiateItem(ViewGroup container, int position) {
			PhotoView photoView = new PhotoView(container.getContext());
			PhotoViewAttacher attacher = new PhotoViewAttacher(photoView);
			Glide.with(activity).load(imageUrls.get(position)).into(attacher);

			// Now just add PhotoView to ViewPager and return it
			container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

			return photoView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

	}
}
